<?php
namespace app\shop\controller\admin;

use app\admin\library\AdminController;

class Variant extends AdminController{

    public function __construct(){
        parent::__construct();
        $this->power('shop.setting.variant');
    }

    public function onIndex(){
        $this->pageTitle = '型号管理';
        $this->pageIcon = 'fa-sliders';
        return $this->display();
        
    }

    public function onList(){
        $variant =  $this->model('Variant');
        return $variant->getData();
    }

    public function onSave(){
        $variant_id = $this->post('variant_id',$this->check()->type('int'));
        $data['title'] =  $this->post('title',$this->check()->type('string'));
        $variant =  $this->model('Variant');
        if ($variant_id){
            
            if ($variant->updateId($variant_id,$data)){
                return true;
            }
        }else{
            if ($variant_id = $variant->insert($data)){
                return ['variant_id'=>$variant_id] ;
            }
        }

        throw new \Exception('保存失败');
    }

    public function onCold(){
        $variant_id =  $this->post('id',$this->check()->type('int')->required());
        $cold = $this->post('cold',$this->check()->type('int')->default(0));
        $variant =  $this->model('Variant');
        if ($variant->updateId($variant_id,['cold'=>$cold])){
            
            return true;
        }

        throw new \Exception('修改失败');
    }


    public function onItemList(){
        $variant_id  = $this->post('variant_id',$this->check()->type('int')->required());
        $variantItem = $this->model('VariantItem');
        $list = $variantItem->where('variant_id',$variant_id)->order('sort','asc')->getData();
        return $list;
    }

    public function onSaveItem(){
        $variant_id  = $this->post('variant_id',$this->check()->type('int')->required());
        $item_id     = $this->post('item_id',$this->check()->type('int'));

        $data['title'] = $this->post('title',$this->check()->type('string')->required());
        $data['image'] = $this->post('image',$this->check()->type('int')->default(0));


        $variantItem = $this->model('VariantItem');


        if ($item_id){
            if ($variantItem->updateId($item_id,$data)){
                return true;
            }
        }else{
            $data['variant_id'] = $variant_id;
            $data['sort'] =  $variantItem->where('variant_id',$variant_id)->max('sort') + 1;
            if ($variantItem->insert($data)){
                return true;
            }
        }

        throw new \Exception('保存失败');
    }


    public function onFieldResource(){
        $variant_id = $this->post('variant_id',$this->check()->type('int')->required());
        $goods_variant_id = $this->post('goods_variant_id',$this->check()->type('int'));

        $re = [];

        $variant = $this->model('Variant');
        $re['variant'] =  $variant->getId($variant_id);

        if (!$re['variant']){
            throw new \Exception('数据不存在');
        }

        $variantItem = $this->model('VariantItem');
        $re['variant_item'] = $variantItem->where('variant_id',$variant_id)->order('sort','asc')->getData();
        

        if ($goods_variant_id){
            $goods = $this->model('Goods');
            $re['goods'] =[];

            $goodsList =  $goods->where('goods_variant_id',$goods_variant_id)->getData();
            if (count($goodsList)>0){
                foreach($goodsList as $goodsItem){
                    $param = json_decode($goodsItem['variant_param'],true);
                    $goodsItem['variant_param'] = $param['value']??[];
                    $re['goods'][] = $goodsItem;
                }
            }

        }
        
        return $re;
    }

    public function onItemSort(){
        $variant_id =  $this->post('variant_id',$this->check()->type('int')->required());
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $variantItem = $this->model('VariantItem');

        $variantItem->where('variant_id',$variant_id)->switchSort($ids,'sort','asc');

        return true;
    }

    public function onExistGoods(){
        $variant_id = $this->post('variant_id',$this->check()->type('int')->required());
        $page = $this->post('page',$this->check()->type('int')->default(1));
        $goodsVariant =$this->model('GoodsVariant');
        $goods = $this->model('Goods');
        $list = $goodsVariant->where('variant_id',$variant_id)->page($page,10)->order($goodsVariant->key,'desc')->getData();
        foreach($list as $k=>$v){
            $goodsList = $goods->field(['title','picture_main','variant_param'])->where($goodsVariant->key,$v[$goodsVariant->key])->getData();
            
            if (count($goodsList)>0){
                $list[$k]['goods_title'] = $goodsList[0]['title'];
                 
                foreach($goodsList as $goodsItem){
                    $param = json_decode($goodsItem['variant_param'],true);
                    $goodsItem['variant_param'] = $param['value']??[];
                    $list[$k]['goods_list'][] = $goodsItem;
                }
            }
        }

        return ['list'=>$list,'total'=>$goodsVariant->total,'count'=>$goodsVariant->pageCount];
    }
}